package org.anupta.game.server.game.packet.account.transfert;

import org.anupta.game.entity.dao.DAO;
import org.anupta.game.entity.entities.Account;
import org.anupta.game.main.Main;
import org.anupta.game.server.game.client.GameClient;
import org.anupta.game.server.game.packet.GamePacket;
import org.anupta.game.server.game.packet.account.transfert.answer.TransfertAgreed;
import org.anupta.game.server.game.packet.account.transfert.answer.TransfertError;

public class AccountTransfert extends GamePacket {
	private int accId = 0;

	public AccountTransfert(GameClient client, String message) {
		super(client, message);
		unpack();
		doPacket();
	}

	@Override
	public void unpack() {
		accId = Integer.parseInt(packetIn.substring(2));
	}

	@Override
	public void pack() {
	}

	@Override
	public void doPacket() {
		if(Main.game.containAccWaiting(accId)){
			Account acc = DAO.getDao(Account.class).find(accId);
			if(acc != null){
				client.setAccount(acc);
				Main.game.remAccWaiting(accId);
				new TransfertAgreed(client);
				return;
			}else{
				new TransfertError(client);
				return;
			}
		}else{
			new TransfertError(client);
			return;
		}
	}
}
